<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/23
 * Time: 15:49
 * 主要作为数据处理类
 */
namespace app\home\controller;
use app\models\Bases;
use think\Config;

class Information extends Base{
    /**
     * @return false|\PDOStatement|string|\think\Collection
     * 获取栏目信息
     */
    public function getType(){
        $column = new Bases('column');
        $result = $column->selectData("status = 1","","sort  asc","");
        return $result;
    }

    /**
     * @param int $type
     * @param string $column_id
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * 获取对应栏目的数据
     */
    public function getTypeData($type = 1,$column_id = ''){
        $article = new Bases('article');       //实例化核心内参
        $shares  = new Bases('shares');        //实例化每日牛股
        $play    = new Bases('play');          //实例化视频回放
        $advert  = new Bases('advert_column'); //多图与单图广告
        if($type == $this->TYPE_CODE ){
            $result = $article->selectData("status = 2","","id desc");//查询核心内参的内容
        }
        if($type == $this->TYPE_STOCK){
            $start_time = strtotime(date('Y-m-d'),time());
            $end_time = $start_time + 3600 * 24;
            $result = $shares->selectData("type_id = $column_id and recommend_time < $end_time and `order` != 0","","recommend_time desc,`order` asc","");
        }
        if($type == $this->TYPE_ADVERT){
            $result = $advert->joinData("a",[['advert b','a.advert_id = b.advert_id','left']],"a.column_id = $column_id and b.status = 1","b.*");
        }
        if($type == $this->TYPE_SPLENDID){
            $result = $play->selectData("type_id = $column_id","","pid desc");
        }
        return $result;
    }

    /**
     * @param array $result
     * @return array
     * 处理每日牛股的数据
     */
    public function everyStock($result = []){
        $shares_user =  new Bases('shares_user');
        $uid = intval(checkLogin());
        $reading = $shares_user->selectData("uid = $uid");
        //循环对比
        foreach($result as $k=>$v){
            $result[$k]['is_reading'] = 0;
            foreach($reading as $k1=>$v1){
                if($v['shares_id'] == $v1['shares_id']){
                    $result[$k]['is_reading'] = 1;
                }
            }
        }
        return $result;
    }

    /**
     * @return int|string
     * 计算该用户本次应该扣除多少积分
     */
    public function deductIntegral(){
        $shares_user =  new Bases('shares_user');
        $uid = intval(checkLogin());
        $today_start_time = strtotime(date('Y-m-d'));
        $today_end_time   = $today_start_time + 3600 * 24;
        $reading_count = $shares_user->count("uid = $uid and create_time >= $today_start_time and create_time < $today_end_time");//获取阅读数
        $deductIntegral =  ($reading_count + 2) * Config::get('integral.reading');//计算积分
        return $deductIntegral;
    }

    /**
     * @return array|false|mixed|\PDOStatement|string|\think\Modelg
     * 获取公告内容
     */
    public function getAnnouncement(){
        $config = new Bases('config');
        return $config->find("cname = 'chat_announcement'");
    }
}